package com.anysoft.service;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.anysoft.MentorApplication;
import com.anysoft.utils.MentorLogger;
import com.aspsine.multithreaddownload.DownloadConfiguration;
import com.aspsine.multithreaddownload.DownloadManager;
import com.blankj.utilcode.util.Utils;
import com.anysoft.event.InitSuccessEvent;
import com.anysoft.greenDAO.db.DbCore;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.TbsDownloader;
import com.tencent.smtt.sdk.TbsListener;
import com.zhy.autolayout.config.AutoLayoutConifg;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import ren.yale.android.cachewebviewlib.CacheWebView;

/**
 * Created by lty on 2018/1/28.
 * 使用service来做初始化应用所需的三方库
 */

public class InitializeService extends IntentService {
    private static final String ACTION_INIT_WHEN_APP_CREATE = "com.anysoft.service.action.init";

    public InitializeService() {
        super("InitializeService");
    }

    public static void start(Context context) {
        MentorLogger.Loge("start");
        Intent intent = new Intent(context, InitializeService.class);
        intent.setAction(ACTION_INIT_WHEN_APP_CREATE);
        context.startService(intent);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            MentorLogger.Loge("onHandleIntent");
            String action = intent.getAction();
            if (ACTION_INIT_WHEN_APP_CREATE.equals(action)) {
                performInit();
            }
        }
    }

    private void performInit() {
        MentorLogger.Loge("performInit前" + System.currentTimeMillis());
        Context context = MentorApplication.context;
        AutoLayoutConifg.getInstance().useDeviceSize();
        initTBS();
        DbCore.init(context);
        Utils.init(context);
        //初始化下载配置
        initDownloader();
//        CrashUtils.init(DiskUtils.CACHE_LOG_PATH);
        File cacheFile = new File(this.getCacheDir(), "icbc_caches");

        CacheWebView.getCacheConfig().init(context, cacheFile.getAbsolutePath(), 1024 * 1024 * 100, 1024 * 1024 * 10)
                .enableDebug(true);//100M 磁盘缓存空间,10M 内存缓存空间
        EventBus.getDefault().post(new InitSuccessEvent(true));
        MentorLogger.Loge("performInit后" + System.currentTimeMillis());
    }

    private void initTBS() {
        // 搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核。
        TbsDownloader.needDownload(getApplicationContext(), false);
        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

            @Override
            public void onViewInitFinished(boolean arg0) {
                // TODO Auto-generated method stub
                Log.d("apptbs", " onViewInitFinished is " + arg0);
            }

            @Override
            public void onCoreInitFinished() {
                // TODO Auto-generated method stub

            }
        };
        QbSdk.setTbsListener(new TbsListener() {
            @Override
            public void onDownloadFinish(int i) {
                Log.d("apptbs", "onDownloadFinish");
            }

            @Override
            public void onInstallFinish(int i) {
                Log.d("apptbs", "onInstallFinish");
            }

            @Override
            public void onDownloadProgress(int i) {
                Log.d("apptbs", "onDownloadProgress:" + i);
            }
        });

        QbSdk.initX5Environment(getApplicationContext(), cb);
    }


    private void initDownloader() {
        DownloadConfiguration configuration = new DownloadConfiguration();
        configuration.setMaxThreadNum(10);
        configuration.setThreadNum(3);
        DownloadManager.getInstance().init(getApplicationContext(), configuration);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        MentorLogger.Loge("onCreate");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        MentorLogger.Loge("onStart");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MentorLogger.Loge("onDestroy");
    }
}
